<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\home\model\page\circle;

use app\api\model\dao\Circle;
use app\api\model\dao\CircleCategory;
use app\api\model\dao\UserAccount;
use app\api\model\data\circle\CircleData;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;

class GetCircleList extends PageBase
{

    private $objCircle;
    private $objCircleCategory;
    private $objCircleData;
    private $objUserAccount;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objCircleCategory = new CircleCategory();
        $this->objCircleData = new CircleData();
        $this->objUserAccount = new UserAccount();

    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            header('Location:/home/loginpage');
            exit;
        }

        $conds = [];

        // 1、参数校验
        if (isset($param['cate_id']) && !empty($param['cate_id'])) {
            $conds['category_id'] = ['=', $param['cate_id']];
        }

        if (isset($param['delete'])) {
            $conds['is_delete'] = ['=', $param['delete']];
        }

        if (isset($param['approve_status'])) {
            $conds['approve_status'] = ['=', $param['approve_status']];
        }

        $conds['is_delete'] = Flag::CONST_CIRCLE_DELETE_NOT;
        $obj = $this->objCircle->getCircleList($conds, [], $this->limit, ' create_time desc');
        if (false === $obj) {
            return $this->getRes(Error::ERR_SYS, 'get circle list fail. [conds: ' . json_encode($conds) . ']');
        }
        $ret = $obj->all();
        $cate_id = [];
        $cateid2Cate = [];
        $userid = [];
        $userid2User = [];

        foreach ($ret as $circle) {
            //$cate_id[] = $circle['category_id'];
            //$userid[] = $circle['creator'];
            $cate_id[] = $circle->category_id;
            $userid[] = $circle->creator;
        }

        if (!empty($cate_id)) {
            $conds = [
                'id' => ['in', $cate_id],
            ];
            $cate_ret = $this->objCircleCategory->getCircleCategory($conds);
            if (false === $cate_ret) {
                return $this->getRes(Error::ERR_SYS, 'get circle category fail. [cate_ids: ' . json_encode($cate_id) . ']');
            }
            foreach ($cate_ret as $cate) {
                $cateid2Cate[$cate['id']] = $cate;
            }
        }

        if (!empty($userid)) {
            $user_ret = $this->objUserAccount->getUserAccountByUserids($userid);
            if (false === $user_ret) {
                return $this->getRes(Error::ERR_SYS, 'get user_account fail. [userids: ' . json_encode($userid) . ']');
            }
            foreach ($user_ret as $user) {
                $userid2User[$user['id']] = $user;
            }
        }
        $this->data['list'] = $this->composeData($ret, $cateid2Cate, $userid2User);
        $this->data['html_page'] = $obj->render();
        return $this->getRes();
    }

    /**
     * @param $arrCircle
     * @param $arrCateid2Cate
     * @param $userid2User
     * @return array
     */
    public function composeData($arrCircle, $arrCateid2Cate, $userid2User)
    {
        if (empty($arrCircle) || !is_array($arrCircle)) {
            return [];
        }
        $data = [];

        foreach ($arrCircle as $circle) {
            $cate_name = isset($arrCateid2Cate[$circle['category_id']]) && !empty($arrCateid2Cate[$circle['category_id']]) ? $arrCateid2Cate[$circle['category_id']]['name'] : '-';
            $approve_status = Flag::CONST_CIRCLE_APPROVE == $circle['approve_status'] ? '待审核' : (Flag::CONST_CIRCLE_APPROVE_PASS == $circle['approve_status'] ? '通过' : '驳回');
            $is_delete = Flag::CONST_CIRCLE_DELETED == $circle['is_delete'] ? '已删除' : '未删除';
            $username = isset($userid2User[$circle['creator']]) && !empty($userid2User[$circle['creator']]) ? $userid2User[$circle['creator']]['nick_name'] : '-';
            $data[] = [
                'cate_id' => $circle['category_id'],
                'cate_name' => $cate_name,
                'cir_id' => $circle['id'],
                'name' => $circle['name'],
                'reason' => $circle['reason'],
                'approve_status' => $approve_status,
                'delete' => $is_delete,
                'creator' => $username,
                'label' => $circle['label'],
                'icon' => $circle['icon'],
                'update_time' => date('Y-m-d H:i:s', $circle['update_time']),
            ];
        }

        return $data;
    }
}
